import {
  addTag,
  getTag
} from '../../../utils/api'
const app = getApp()
import Dialog from '../../../miniprogram_npm/@vant/weapp/dialog/dialog';
Page({
  /**
   * 页面的初始数据
   */
  data: {
    interest: [],
    arr: [],
    avatarUrl: '',
    nickName: '',
    country: '',
    tagId:null,
  },
  getUserInfo(event) {
    console.log(event.detail);
  },
  onClose() {
    this.setData({
      show: false
    });
  },
  //关闭tag标签
  close(e) {
    let name = e.target.dataset.item;
    let target = e.target.dataset.item.name;
    let interest = this.data.interest;
    let userId = app.globalData.userId;
    interest = interest.filter((i) => {
      return i.name != target;
    })
    let delId = interest.map(v => v.id)
    delId = delId.join(',')
    this.setData({
      interest,
      tagId: delId
    })
    addTag(userId,delId).then((res)=>{
      console.log('删除成功',res);
    })
    this.changeChildValue(name)
  },
  // 关闭后标签更新子组件，自动增加子组件中的值,发送给后端
  changeChildValue(data) {
    const child = this.selectComponent('.mine-options');
    let items = child.data.items;
    data.checked = ""
    items.push(data)
    child.setData({
      items,
      arr: []
    })
  },
  toEditorPage: function () {
    wx.navigateTo({
      url: '../mine_editor/mine_editor',
    })
  },
  dialog() {
    Dialog.confirm({
        message: '弹窗内容',
      })
      .then(() => {
        const child = this.selectComponent('.mine-options');
        let allTags = child.data.allTags;
        let arr = this.data.arr;
        let interest = this.data.interest;
        let a = []
        allTags.forEach((i,j)=>{
          for(let t =0;t<arr.length;t++){
            if(i.name===arr[t]){
              a.push(i)
            }
          }
        })
        // console.log('a',a);
        // console.log('arr',arr);
        // console.log('allTags',allTags);
        // console.log('interest',interest);
        interest.push(...a)
        this.setData({
          interest,
          arr: []
        })
      }).then(() => {
        let tagData = this.toChangeChild()
        return tagData
      }).then((tagData) => {
        let {
          items,
          oldItems
        } = tagData;
        let idList = items.map(v => v.id)
        let oldIdList = oldItems.map(v => v.id)
        var newArr = [];
        for (var i = 0; i < oldIdList.length; i++) {
          if (this.exist(oldIdList[i], idList)) {
            newArr[newArr.length] = oldIdList[i];
          }
        }
        return newArr
      }).then((res) => {
        let userId = app.globalData.userId
        let tagId = this.data.tagId;
        tagId=tagId.concat(res).join(',')
        addTag(userId,tagId).then((res) => {
          console.log('添加标签',res);
          this.setData({
            tagId
          })
        })
      }).catch(() => {})
  },
  //数组去重
  exist(num, arr1) {
    for (var j = 0; j < arr1.length; j++) {
      if (num === arr1[j]) {
        return false; //如果传过来的元素在arr1中能找到相匹配的元素，我们返回fasle
      }
    }
    return true;
  },
  //深拷贝
  deepClone(obj) {
    let _temp, result;
    _temp = JSON.stringify(obj);
    result = JSON.parse(_temp);
    return result
  },
  //添加功能代码，获取子组件中的值，动态减少子组件中的值
  toChangeChild() {
    const child = this.selectComponent('.mine-options')
    let items = child.data.items;
    let oldItems = this.deepClone(items)
    items = items.filter((i) => {
      return i.checked != true
    })
    child.setData({
      items,
      arr: []
    })
    return {
      items,
      oldItems
    };
  },
  addInterest(e) {
    var that = this
    that.setData({
      arr: e.detail
    })
  },
  //获取用户标签
  getUserTag() {
    let userId = app.globalData.userId
    getTag(userId).then((res) => {
      console.log('获取用户标签', res);
      let tagId = res.data.map(v=>v.id)
      this.setData({
        interest: res.data,
        tagId
      })
    })
  },
  onLoad: function () {
    this.getUserTag()
    let userInfo = wx.getStorageSync('userInfo')
    this.setData({
      avatarUrl: userInfo.avatarUrl,
      nickName: userInfo.nickName,
      country: userInfo.country,
    })
  }
})